﻿using Circus.Utility;
using Circus.Utility.Json;
using System;
using System.Data;
using System.Data.SqlClient;

namespace Teal.Club {

    /// <summary>
    /// 提供用户评论、赞、收藏等相关接口。
    /// </summary>
    public static class VoteController {

        #region 评论

        /// <summary>
        /// 添加一个评论。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <param name="content">评论的内容，最长 3000 字符。</param>
        /// <returns>返回标准 JSON 对象。</returns>
        public static object addComment(TableType tableType, long tableId, string content) {

            var createTime = DateTime.Now;

            var id = DbHelper.executeProcedureScalar("comment_add",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@content", content),
                new SqlParameter("@createTime", createTime));

            return new {
                code = 0,
                id = id,
                user = new {
                    id = UserController.currentUser.id,
                    name = UserController.currentUser.name,
                    avatar = UserController.currentUser.avatar,
                    signature = UserController.currentUser.signature
                },
                createTime = createTime
            };

        }

        /// <summary>
        /// 更新评论。
        /// </summary>
        /// <param name="id">要更新的评论序号。</param>
        /// <param name="content">要更新的评论内容，最长 3000 字符。</param>
        /// <returns>返回标准 JSON 对象。</returns>
        public static object updateComment(long id, string content) {

            var createTime = DateTime.Now;

            if (DbHelper.executeProcedureNonQuery("comment_update",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id),
                new SqlParameter("@content", content),
                new SqlParameter("@createTime", createTime)) == 0) {
                return new {
                    code = 1,
                    msg = "评论不存在或已删除"
                };
            }

            return new {
                code = 0,
                id = id,
                user = new {
                    id = UserController.currentUser.id,
                    name = UserController.currentUser.name,
                    avatar = UserController.currentUser.avatar,
                    signature = UserController.currentUser.signature
                },
                createTime = createTime
            };

        }

        /// <summary>
        /// 删除评论。
        /// </summary>
        /// <param name="id">要删除的评论序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string deleteComment(long id) {

            if (DbHelper.executeProcedureNonQuery("comment_delete",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id)) == 0) {
                return "{\"code\":1,\"msg\":\"评论不存在或已删除，或者您没有权限操作\"}";
            }

            return CommonResponse.success;
        }

        /// <summary>
        /// 回复指定的评论。
        /// </summary>
        /// <param name="id">要回复的评论序号。</param>
        /// <param name="content">要回复的评论内容。</param>
        /// <returns>返回标准 JSON 对象。</returns>
        public static object replyComment(long id, string content) {
            return addComment(TableType.comment, id, content);
        }

        /// <summary>
        /// 查询所有评论。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <param name="pageNumber">当前页码。页码从 1 开始。</param>
        /// <param name="pageSize">每页显示的条数。</param>
        /// <param name="orderByType">排序的类型。</param>
        /// <returns>返回标准 JSON 对象。</returns>
        public static object queryComments(TableType tableType, long tableId, int pageNumber, int pageSize = 5, CommentOrderByType orderByType = CommentOrderByType.latest) {

            JsonArray comments = new JsonArray(pageSize);

            var totalCountParam = DbHelper.createOutputParameter("@totalCount", SqlDbType.Int);

            using (var reader = DbHelper.executeProcedureReader("comment_query",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@pageNumber", pageNumber),
                new SqlParameter("@pageSize", pageSize),
                new SqlParameter("@orderByType", orderByType),
                totalCountParam)) {

                bool hasNextValue = reader.Read();
                while (hasNextValue) {

                    var id = reader.getLong("id");

                    var replies = reader.isDBNull("Reply_id") ? null : new JsonArray();

                    comments.Add(new {
                        id = id,
                        content = reader.getString("content"),
                        createTime = reader.getDateTime("createTime"),
                        likeCount = reader.getInt("likeCount"),
                        dislikeCount = reader.getInt("dislikeCount"),
                        user = new {
                            id = reader.getLong("userId"),
                            name = reader.getString("userName"),
                            avatar = reader.getString("userAvatar"),
                            signature = reader.getString("userSignature"),
                        },
                        replies = replies
                    });

                    if (replies != null) {
                        do {
                            replies.Add(new {
                                id = reader.getLong("Reply_id"),
                                content = reader.getString("Reply_content"),
                                createTime = reader.getDateTime("Reply_createTime"),
                                user = new {
                                    id = reader.getLong("Reply_userId"),
                                    name = reader.getString("Reply_userName"),
                                    avatar = reader.getString("Reply_userAvatar"),
                                    signature = reader.getString("Reply_userSignature"),
                                },
                            });
                        } while ((hasNextValue = reader.Read()) && reader.getId() == id);
                    }

                    hasNextValue = reader.Read();

                }

            }

            return new {
                code = 0,
                totalCount = totalCountParam.Value,
                comments = comments
            };
        }

        /// <summary>
        /// 赞一个评论。
        /// </summary>
        /// <param name="id">要赞的评论序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string likeComment(long id) {

            if (DbHelper.executeProcedureNonQuery("comment_like_add",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id),
                new SqlParameter("@createTime", DateTime.Now)) == 0) {
                return "{\"code\":1,\"msg\":\"评论不存在或已删除\"}";
            }

            return CommonResponse.success;
        }

        /// <summary>
        /// 踩一个评论。
        /// </summary>
        /// <param name="id">要踩的评论序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string dislikeComment(long id) {

            if (DbHelper.executeProcedureNonQuery("comment_dislike_add",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id),
                new SqlParameter("@createTime", DateTime.Now)) == 0) {
                return "{\"code\":1,\"msg\":\"评论不存在或已删除\"}";
            }

            return CommonResponse.success;
        }

        /// <summary>
        /// 取消赞一个评论。
        /// </summary>
        /// <param name="id">要赞的评论序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string removeLikeComment(long id) {

            if (DbHelper.executeProcedureNonQuery("comment_like_remove",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id)) == 0) {
                return "{\"code\":1,\"msg\":\"评论不存在或已删除\"}";
            }

            return CommonResponse.success;
        }

        /// <summary>
        /// 取消踩一个评论。
        /// </summary>
        /// <param name="id">要踩的评论序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string removeDislikeComment(long id) {

            if (DbHelper.executeProcedureNonQuery("comment_dislike_remove",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@id", id)) == 0) {
                return "{\"code\":1,\"msg\":\"评论不存在或已删除\"}";
            }

            return CommonResponse.success;
        }

        #endregion

        #region 评论

        /// <summary>
        /// 查询所有的赞和踩。
        /// </summary>
        /// <returns></returns>
        public static object queryLikes() {
            return null;
        }

        /// <summary>
        /// 对指定内容添加一个赞。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string addLike(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容添加一个踩。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string addDislike(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容进行收藏。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string addFavorite(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("vote_addFavorite",
                new SqlParameter("@userId", UserController.currentUser.id),
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@createTime", DateTime.Now)) == 0) {
                return "{\"code\":1,\"msg\":\"您已收藏过了\"}";
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容添加一个关注。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string addWatch(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容取消一个赞。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string removeLike(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容取消一个踩。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string removeDislike(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        /// <summary>
        /// 对指定内容进行取消收藏。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static string removeFavorite(TableType tableType, long tableId) {

            if (DbHelper.executeProcedureNonQuery("like_add",
                new SqlParameter("@tableType", tableType),
                new SqlParameter("@tableId", tableId),
                new SqlParameter("@userId", UserController.currentUser.id)) == 0) {
                return CommonResponse.updateError;
            }

            return CommonResponse.success;

        }

        #endregion
        
        #region 评论

        /// <summary>
        /// 判断当前用户是否已对指定内容进行收藏。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static bool hasLikeCore(TableType tableType, long tableId) {

            return true;
        }

        /// <summary>
        /// 判断当前用户是否已对指定内容进行收藏。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static bool hasDislikeCore(TableType tableType, long tableId) {

            return true;
        }

        /// <summary>
        /// 判断当前用户是否已对指定内容进行收藏。
        /// </summary>
        /// <param name="tableType">表格编号。</param>
        /// <param name="tableId">表格内的序号。</param>
        /// <returns>返回标准 JSON 字符串。</returns>
        public static bool hasFavoriteCore(TableType tableType, long tableId) {

            return true;
        }

        #endregion

    }

    /// <summary>
    /// 表示表类型。
    /// </summary>
    public enum TableType : byte {

        /// <summary>
        /// 评论表。
        /// </summary>
        comment,

        /// <summary>
        /// 用户表。
        /// </summary>
        user,

        /// <summary>
        /// 文章表。
        /// </summary>
        article,

        /// <summary>
        /// 课程表。
        /// </summary>
        course,

    }

    /// <summary>
    /// 表示评论排序类型枚举。
    /// </summary>
    public enum CommentOrderByType : byte {

        /// <summary>
        /// 最新。
        /// </summary>
        latest,

        /// <summary>
        /// 最早。
        /// </summary>
        oldest,

        /// <summary>
        /// 最热。
        /// </summary>
        hotest,

    }
}